﻿using Mod.Search.Models;
using Orchard;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.Localization;
using Orchard.UI.Notify;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Mod.Search.Drivers
{
    public class SearchSiteSettingsDriver : ContentPartDriver<SearchSiteSettings>
    {
        private const string TemplateName = "Parts/SearchSiteSettings";
        private readonly INotifier notifier;

        public SearchSiteSettingsDriver(INotifier notifier)
        {
            this.notifier = notifier;
            T = NullLocalizer.Instance;
        }

        public Localizer T { get; set; }

        protected override DriverResult Editor(SearchSiteSettings part, dynamic shapeHelper)
        {
            return ContentShape("Parts_SearchSiteSettings",
                   () => shapeHelper.EditorTemplate(
                       TemplateName: TemplateName,
                       Model: part,
                       Prefix: Prefix))
                       .OnGroup("search");
        }

        protected override DriverResult Editor(SearchSiteSettings part, IUpdateModel updater, dynamic shapeHelper)
        {
            if (!updater.TryUpdateModel(part, Prefix, null, null)) 
            {
                notifier.Error(T("Error setting Search Site Settings!"));
            }

            return Editor(part, shapeHelper);
        }

    }
}